package com.ham.mud.commands.socials;

import com.ham.mud.Colors;
import com.ham.mud.ServerConnection;
import com.ham.mud.TargetableService;
import com.ham.mud.characters.MudCharacter;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.player.Player;
import com.ham.mud.commands.PlayerCommand;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by hlucas on Jun 16, 2011 at 5:18:46 PM
 */
public class SocialCommand extends PlayerCommand {
    public static final List<String> socials = new ArrayList<String>();
    static {
        socials.add("smile");
        socials.add("frown");
        socials.add("chuckle");
        socials.add("grin");
        socials.add("snicker");
        socials.add("peer");
        socials.add("laugh");
        socials.add("whistle");
        socials.add("poke");
        socials.add("yawn");
        socials.add("giggle");
        socials.add("ponder");
        socials.add("nod");
        socials.add("wave");
        socials.add("shrug");
        socials.add("cackle");
        socials.add("sneer");
        socials.add("sigh");
        socials.add("cry");
        socials.add("pout");
        socials.add("sniffle");
        socials.add("sigh");
        socials.add("fondle");
        socials.add("carress");
        socials.add("lick");
        socials.add("cower");
        Collections.sort(socials);
    }
    String social;

    public SocialCommand(String social) {
        this.social = social;
    }

    @Override
    public void execute(ServerConnection connection, String[] params) {
        Player player = connection.getPlayer();
        Room room = RoomService.getRoom(player.getZone(), player.getAtX(), player.getAtY());
        String playerName = connection.getPlayer().getName();

        if(params.length == 0) {
            connection.printSolo(getWhatPlayerSees());
            List<ServerConnection> others = room.getOtherConnections(connection);
            for(ServerConnection other : others) {
                other.printSolo(Colors.NEW_LINE + getWhatOtherPlayerSees(playerName));
            }
        } else {
            String target = params[0];
            List<Mob> mobs = room.getMobs();
            List<Player> players = room.getOtherPlayers(connection);

            MudCharacter mudCharacter = (MudCharacter) TargetableService.findTarget(target, mobs, players);
            if(mudCharacter == null) {
                connection.printSolo("You do not see anyone here by the name of \"" + target + "\" to " + getCommandName() + " at.");
                return;
            }
            
            connection.printSolo(getWhatPlayerSees(mudCharacter.getName()));
            List<ServerConnection> others = room.getOtherConnections(connection);
            for(ServerConnection other : others) {
                other.printSolo(Colors.NEW_LINE + getWhatOtherPlayerSees(playerName, mudCharacter.getName(), other.getPlayer().getName()));
            }
        }
    }

    private String getWhatPlayerSees() {
        return "You " + social + ".";
    }

    private String getWhatOtherPlayerSees(String playerName) {
        return playerName + " " + social + "s.";
    }

    private String getWhatPlayerSees(String name) {
        return "You " + social + " at " + name + ".";
    }

    private String getWhatOtherPlayerSees(String playerName, String target, String currentTarget) {
        if(currentTarget.equals(target)) {
            target = "you";
        }
        return playerName + " " + social + "s at " + target + ".";
    }

    @Override
    protected String getCommandName() {
        return "social";
    }
}
